package com.umer.pinban.customer.dialog;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import com.common.bean.MsgBean;
import com.common.utils.EventConstants;
import com.lxj.xpopup.animator.PopupAnimator;
import com.lxj.xpopup.core.CenterPopupView;
import com.umer.pinban.R;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;

public class DialogTwo extends CenterPopupView {

    private DatePicker startTime,endTime;
    private TextView tv1,tv2,tv_cancle,tv_ok;
    private String menstrualStartDate = "";
    private String menstrualEndDate = "";
    private Context context;
    private EventBus eventBus;

    public DialogTwo(@NonNull Context context) {
        super(context);
        this.context = context;
    }

    // 返回自定义弹窗的布局
    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_date;
    }
    // 执行初始化操作，比如：findView，设置点击，或者任何你弹窗内的业务逻辑
    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate() {
        super.onCreate();
        startTime = findViewById(R.id.st);
        endTime = findViewById(R.id.et);
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv_cancle = findViewById(R.id.tv_cancle);
        tv_ok = findViewById(R.id.tv_ok);
        tv_cancle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss(); // 关闭弹窗
            }
        });
        tv_ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menstrualStartDate.equals("") || menstrualStartDate.equals(" ")) {
                    Toast.makeText(context,"请选择开始时间",Toast.LENGTH_SHORT).show();
                } else if (menstrualEndDate.equals("") || menstrualEndDate.equals(" ")) {
                    Toast.makeText(context,"请选择结束时间",Toast.LENGTH_SHORT).show();
                } else {
                    List<String> times = new ArrayList<>();
                    times.add(menstrualStartDate);
                    times.add(menstrualEndDate);
                    MsgBean msgBean = new MsgBean();
                    msgBean.setFlag(EventConstants.select_time);
                    msgBean.setObject(times);
                    eventBus.post(msgBean);
                    dismiss(); // 关闭弹窗
                }
            }
        });
        startTime.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                int month = startTime.getMonth() + 1;
                menstrualStartDate = "" + startTime.getYear() + "-" + month + "-" +
                        startTime.getDayOfMonth();
                tv1.setText(menstrualStartDate);
            }
        });
        endTime.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                int month = endTime.getMonth() + 1;
                menstrualEndDate = "" + endTime.getYear() + "-" + month + "-" +
                        endTime.getDayOfMonth();
                tv2.setText(menstrualEndDate);
            }
        });
    }
    // 设置最大宽度，看需要而定
    @Override
    protected int getMaxWidth() {
        return super.getMaxWidth();
    }
    // 设置最大高度，看需要而定
    @Override
    protected int getMaxHeight() {
        return super.getMaxHeight();
    }
    // 设置自定义动画器，看需要而定
    @Override
    protected PopupAnimator getPopupAnimator() {
        return super.getPopupAnimator();
    }
    /**
     * 弹窗的宽度，用来动态设定当前弹窗的宽度，受getMaxWidth()限制
     *
     * @return
     */
    protected int getPopupWidth() {
        return 0;
    }

    /**
     * 弹窗的高度，用来动态设定当前弹窗的高度，受getMaxHeight()限制
     *
     * @return
     */
    public void setEventBus(EventBus eventBus) {
        this.eventBus = eventBus;
    }
}